<?php


namespace app\api\controller;


use app\admin\model\ForeknowledgeCollection;
use app\admin\model\ForeknowledgeType;
use think\Controller;
use think\Request;

class Foreknowledge extends Controller
{
    public function getForeknowledgeTypeList(){
        $res = [];

        $foreknowledgeType = new ForeknowledgeType();
        $where['id'] = ['neq',0];
        $rs = $foreknowledgeType->getAll($where);

        $res['data']['code'] = 1;
        $res['data']['list'] = $rs;
        $res['status'] = 200;
        echo json_encode($res);
    }

    public function getForeknowledgeList(Request $request){
        $res = [];
        $data = $request->post();
        $page = $request->get();

        $foreknowledge = new \app\admin\model\Foreknowledge();
        if(!isset($data['type']) or !isset($data['city']) or !isset($page['page']) ){
            $res['status'] = 400;
            echo json_encode($res);die;
        }
        if($data['type'] == 1){
            if(isset($data['where_type']))$where['type'] = $data['where_type'];
        }else{
            $where['title'] = ['like',"%".$data['selected'].'%'];
        }
        $where['status'] = 1;
        $rs = $foreknowledge->getPageAll($where);

        //收藏和购物车状态
        foreach($rs as $j) {
            $j['in_collection'] = 0;
            if (isset($data['user_id'])) {
                //查询收藏
                $whereCollection['user_id'] = $data['user_id'];
                $whereCollection['p_id'] = $j['id'];
                $whereCollection['type'] = 'foreknowledge';
                $collection = new \app\admin\model\Collection();
                if ($collection->getOne($whereCollection)) {
                    $j['in_collection'] = 1;
                }
            }
        }

        if($rs){
            $res['data']['code'] = 1;
            $res['data']['list'] = $rs;
        }else{
            $res['code'] = 0;
        }
        $res['status'] = 200;
        echo json_encode($res);
    }

    public function getForeknowledgeDetails(Request $request){
        $res = [];
        $data = $request->post();

        $foreknowledge = new \app\admin\model\Foreknowledge();
        if(!isset($data['id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $foreknowledge = new \app\admin\model\Foreknowledge();
        $where['id'] = $data['id'];
        $rs = $foreknowledge->getOne($where);
        if($rs){
            //查询是否已添加购物车
            $shopCart = new \app\admin\model\ShopCart();
            $shopWhere['user_id'] = isset($data['user_id']) ? $data['user_id'] : 0;
            $shopWhere['pr_type'] = 1;
            $shopWhere['pr_id'] = $data['id'];
            if($shopCart->getOne($shopWhere)){
                $rs['in_cart'] = 1;
            }else{
                $rs['in_cart'] = '';
            }

            $whereCollection['user_id'] = isset($data['user_id']) ? $data['user_id'] : 0;
            $whereCollection['p_id'] = $data['id'];
            $whereCollection['type'] = 'foreknowledge';
            $collection = new \app\admin\model\Collection();
            if($collection->getOne($whereCollection)){
                $rs['in_collection'] = 1;
            }else{
                $rs['in_collection'] = '';
            }

            $res['data']['code'] = 1;
            $res['data']['list'] = $rs;
        }else{
            $res['data']['code'] = 0;
        }

        $res['status'] = 200;
        echo json_encode($res);
    }
}